package com.bite.blog.controller;

import com.bite.blog.constant.Constants;
import com.bite.blog.model.BlogInfo;
import com.bite.blog.service.BlogService;
import com.bite.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    //@GetMapping
    @RequestMapping(value = "/getList", method = RequestMethod.GET)  //method限制只能GET请求  也可以用@GetMapping
    public List<BlogInfo> getList() {
        log.info("获取博客列表...");  //日志
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")  //根据博客ID返回博客信息
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("getBlogDetail,blogid:{}", blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);

        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token里获取用户信息(之前用户信息已存入token)
        Integer userId = JwtUtils.getIdByToken(token);
        //判断作者是否为登录用户--->一样就可以在当前登录用户修改博客信息
        if (userId != null && userId == blogDetail.getUserId()) {
            blogDetail.setLoginUser(true);
        }
        return blogDetail;
    }

    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {  //使用JSON 加注解
        log.info("添加博客,blogInfo:{}", blogInfo);
        //参数校验
        if (!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {  //为空
            return false;  //也可以返回错误原因,参考usercontroller
        }

        /*
        获取登录的用户信息--从token里获取
        (前端返回的只有title和content,所以要额外获取登录的用户信息一起插入数据库)
        */
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token里获取用户信息(之前用户信息已存入token)
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId == null || userId <= 0) {
            return false;
        }
        blogInfo.setUserId(userId);
        return blogService.insertBlog(blogInfo);
    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客,blogId:{}", blogInfo);
        //参数校验
        if (blogInfo.getId()==null
                || !StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }

        blogService.update(blogInfo);
        return true;

    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId) {
        log.info("删除博客,blogId:{}", blogId);
        if (blogId <= 0) {
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);  //删除操作
        blogService.update(blogInfo);
        return true;
    }

}
